package opera;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOpreation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书！");
        System.out.println("请选择你要删除的图书：");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int a = bookList.getUsedSize();
        int index = -1;
        for (int i = 0; i < a; i++) {
            Book book = bookList.getBooks(i);
            if (book.getName().equals(name)){
                index = i;
                break;
            }
            System.out.println("对不起，没有你要删除的图书。");
        }

        for (int j = index; j < a-1; j++) {
            Book book = bookList.getBooks(j+1);
            bookList.setBooks(j,book);
        }

        bookList.setUsedSize(a-1);
        bookList.setBooks(a-1,null);
        System.out.println("删除成功！");
    }
}
